package easy;

public class Solution_563 {
    private int sum = 0;
    public int findTilt(TreeNode root) {
        dps(root);
        return sum;
    }

    private int dps(TreeNode node) {
        if (node==null) {
            return 0;
        }
        int left = dps(node.left);
        int right = dps(node.right);
        int abs = Math.abs(left - right);
        sum += abs;
        System.out.println("node.val:"+node.val + "; node.left:"+left+"; node.right:"+right+"; node.sum:"+(left+right + node.val));
        return left + right + node.val;
    }

    public static void main(String[] args) {
        Solution_563 model = new Solution_563();
        /*Solution_563.TreeNode root = new Solution_563.TreeNode(1);
        root.left = new Solution_563.TreeNode(2);
        root.right = new Solution_563.TreeNode(3);*/
        /*Solution_563.TreeNode root = new Solution_563.TreeNode(4);
        root.left = new Solution_563.TreeNode(2);
        root.left.left = new Solution_563.TreeNode(3);
        root.left.right = new Solution_563.TreeNode(5);
        root.right = new Solution_563.TreeNode(9);
        root.right.right = new Solution_563.TreeNode(7);*/
        System.out.println(model.findTilt(null));
    }

    public static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;     }
  }
}
